#!/usr/bin/env python

import sys
import vtk

from jolly.datamanagement.vtkPythonMetaImageData  import *




sys.argv.append("C:/S70")
    
if len(sys.argv)<2:
	sys.exit("Usage:\n\t%s <image file>\nExample: \n\t%s [vtkINRIA3D_DATA_DIR]/MRI.vtk\n" 
                 % (sys.argv[0], sys.argv[0]))
    

# Create the standard renderer, render window and interactor
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

# Read image data
image = vtkPythonMetaImageData()
image.ReadSeriesFile(sys.argv[1])
imagedata = image.getDataSet()

scalarrange = imagedata.GetScalarRange()
shift = 0 - scalarrange[0]
scale = 65535.0 / (scalarrange[1]-scalarrange[0])

scaler = vtk.vtkImageShiftScale()
scaler.SetInput(imagedata)
scaler.SetShift(shift)
scaler.SetScale(scale)
scaler.SetOutputScalarTypeToUnsignedShort()
scaler.Update()

# Create transfer mapping scalar value to opacity
opacityTransferFunction = vtk.vtkPiecewiseFunction()
# opacityTransferFunction.AddPoint(20, 0.0)
# opacityTransferFunction.AddPoint(255, 0.2)

# Create transfer mapping scalar value to color
level = 35000
window = 1000.0
v_min = level-window/2
v_max = level+window/2
print v_min, ' ' , v_max
lookuptable = vtk.vtkLookupTable()
lookuptable.SetRange(v_min, v_max)
numColors = lookuptable.GetNumberOfTableValues()
print numColors
scaleOpacity = 1.4
colorTransferFunction = vtk.vtkColorTransferFunction()
colorTransferFunction.RemoveAllPoints()
for i in range(numColors):
    color = lookuptable.GetTableValue(i)
    print color
    colorTransferFunction.AddRGBPoint(v_min + float(i)*(v_max-v_min)/(numColors-1), color[0], color[1], color[2])
opacityTransferFunction.AddPoint(0.0, 0.0)
opacityTransferFunction.AddPoint(v_min, 0.0)
opacityTransferFunction.AddPoint(v_max, 1.0/scaleOpacity)
       

# The property describes how the data will look
volumeProperty = vtk.vtkVolumeProperty()
volumeProperty.SetColor(colorTransferFunction)
volumeProperty.SetScalarOpacity(opacityTransferFunction)
volumeProperty.ShadeOn()
volumeProperty.SetInterpolationTypeToLinear()

# The mapper / ray cast function know how to render the data
compositeFunction = vtk.vtkVolumeRayCastCompositeFunction()
# volumeMapper = vtk.vtkVolumeRayCastMapper()
volumeMapper = vtk.vtkVolumeTextureMapper3D()
# volumeMapper.SetVolumeRayCastFunction(compositeFunction)
volumeMapper.SetInput(scaler.GetOutput())

# The volume holds the mapper and the property and
# can be used to position/orient the volume
volume = vtk.vtkVolume()
volume.SetMapper(volumeMapper)
volume.SetProperty(volumeProperty)

ren.AddVolume(volume)
ren.SetBackground(1, 1, 1)
renWin.SetSize(600, 600)
renWin.Render()

def CheckAbort(obj, event):
    if obj.GetEventPending() != 0:
        obj.SetAbortRender(1)
 
renWin.AddObserver("AbortCheckEvent", CheckAbort)

iren.Initialize()
renWin.Render()
iren.Start()
